package com.myc.subject.domain.handle.subject;

import com.myc.subject.common.enums.IsDeletedFlagCodeEnum;
import com.myc.subject.common.enums.SubjectInfoTypeEnum;
import com.myc.subject.domain.convert.RadioSubjectConverter;
import com.myc.subject.domain.entity.SubjectAnswerBO;
import com.myc.subject.domain.entity.SubjectInfoBO;
import com.myc.subject.domain.entity.SubjectOptionBO;
import com.myc.subject.infra.basic.entity.SubjectRadio;
import com.myc.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选策略类
 * */
@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //单选题目插入
        List<SubjectRadio> subjectRadioList = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option ->{
            SubjectRadio subjectRadio = RadioSubjectConverter.INSTANCE.convertBoToRadio(option);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagCodeEnum.UN_DELETED.getCode());
            subjectRadioList.add(subjectRadio);
        });
        subjectRadioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        //构造SubjectRadio对象
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(Long.valueOf(subjectId));
        //查询题目并进行转换
        List<SubjectRadio> subjectRadioList = subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOList = RadioSubjectConverter.INSTANCE.convertEntityToBoList(subjectRadioList);
        //构造回答信息
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
